package com.hex.ds.code.detection.pack.service.impl;


import cn.hutool.core.util.ReUtil;

import java.util.List;
import java.util.regex.Pattern;

import com.hex.ds.code.detection.pack.service.ISQLExtractor;
import org.springframework.stereotype.Component;

/**
 * @Package: com.hex.hdtp.scf.impala.analyze.py.service.impl
 * @ClassName PythonSQLExtractor
 * @Description: python中抽取sql
 * @Author: Dong shiyi
 * @Date 2023/9/8 18:07
 * @Version v1.0
 **/
@Component
public class PythonSQLExtractor implements ISQLExtractor {

  private static final Pattern PATTERN_SQL = Pattern.compile("((?im)(-*\\b(INSERT|UPDATE|DELETE|SELECT|CREATE|ALTER|DROP|TRUNCATE|ANALYZE|WITH|UPSERT|SET|REFRESH|COMPUTE)[\\s\\S]*?)(;(?![\'\\w\\u4e00-\\u9fa5])))");

  @Override
  public List<String> extractor(String sourceStr) {
    List<String> sqls = ReUtil.findAllGroup1(PATTERN_SQL, sourceStr.concat(";"));
    return sqls;
  }
}
